package edu.cmu.cs211.exprtree.tests;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import org.junit.Test;
import static edu.cmu.cs211.exprtree.tests.ExpressionTestUtil.*;

public class SubtractExpressionTest {

	  @Test
	  public void testCfold0()
	  {
	    cfoldtest("6-5", "1");
	    cfoldtest("5-x", "5-x");
	    cfoldtest("x-5", "x-5");
	  }
	  
	  @Test
	  public void testCfold4() {
		cfoldtest("(x-1)-(x-0)-(x-0)", "(x-1)-x-x");
	    cfoldtest("(6-5)-(6-5)", "0");
		cfoldtest("((x-1)-(0))", "x-1");  
	    cfoldtest("((x-1)-(0-0))", "x-1");
	    cfoldtest("(0-0)-(x-1)", "-(x-1)");
	    cfoldtest("20-1-1-1-1-1-1-1-1-1-1", "10");
	  }
	  
		@Test
		public void evalNoVars() {
			/* Construct the variable map */
			Map<String, BigDecimal> map = new HashMap<String, BigDecimal>();
			
			/* test the evaluate function with no variables */
			ExpressionTestUtil.evaluatetest("7-5", "2", map);
			ExpressionTestUtil.evaluatetest("(7-5)-(6-5)", "1", map);
			ExpressionTestUtil.evaluatetest("20-1-1-1-1-1-1-1-1-1-1", "10", map);
		}
		
		@Test
		public void evalVars() {
			/* Construct the variable map (maybe we could do this dynamically in a loop for 
			 * a more "stressful" test?)
			 */
			Map<String, BigDecimal> map = new HashMap<String, BigDecimal>();
			map.put("x", new BigDecimal("7"));
			
			/* test the evalaute function with the current variable mapping */
			ExpressionTestUtil.evaluatetest("5-x", "-2", map);
			ExpressionTestUtil.evaluatetest("x-5", "2", map);
			ExpressionTestUtil.evaluatetest("0-x", "-7", map);
			ExpressionTestUtil.evaluatetest("0.0-x", "-7.0", map);
			ExpressionTestUtil.evaluatetest("0.1-x", "-6.9", map);
			ExpressionTestUtil.evaluatetest("x-0", "7", map);
			ExpressionTestUtil.evaluatetest("x-0.0", "7.0", map);
			ExpressionTestUtil.evaluatetest("x-0.1", "6.9", map);
			ExpressionTestUtil.evaluatetest("(0-0)-x", "-7", map);
			ExpressionTestUtil.evaluatetest("x-(0-0)", "7", map);
			ExpressionTestUtil.evaluatetest("(0-1)-x", "-8", map);
			ExpressionTestUtil.evaluatetest("x-(1-0)", "6", map);
			ExpressionTestUtil.evaluatetest("(x-1)-(0-0)", "6", map);
			ExpressionTestUtil.evaluatetest("(0-0)-(x-1)", "-6", map);
		}
}
